<template>
  <!-- 页面-组织架构-组织名称-多语言弹框 -->
  <div>
    <Form :label-width="75">
      <Row :gutter="20" class="code-row-bg" v-for="item in items" :key="item.langCode">
        <Col span="24">
          <Form-item :label="item.langDesc">
            <Input v-model="item.orgName"  :maxlength="30" />
          </Form-item>
        </Col>
      </Row>
    </Form>
  </div>
</template>
<script>
import locales from "../locales.js";
import { getOrgNameLangList, updateOrgNameLangList } from "@/api/orgApi.js";
export default {
  name: "orgname-edit",
  mixins: [locales],
  props: {
    orgId: {
      type: String,
      default() {
        return "";
      }
    }
  },
  created: function() {
    this.$emit("SetPageWidth", 400);
    this.getOrgLang();
    this.$emit("SetTitle", this.$t("module.orgManage.orgNameEdit"));
    this.$emit("SetPageActions", [
      {
        text: this.$t("common.save"),
        theme: "primary",
        icon: "edit",
        action: "SetManuLAng",
        handle: this.updateOrgLang
      }
    ]);
  },
  data() {
    return {
      items: []
    };
  },
  methods: {
    // 获取多语言
    getOrgLang() {
      getOrgNameLangList(this.orgId).then(response => {
        let data = response.data;
        this.items = data.map(item => {
          return {
            ...item,
            langDesc:
              item.langCode == "zh-CN"
                ? this.$t("module.orgManage.zhCN")
                : item.langCode == "zh-TW"
                ? this.$t("module.orgManage.zhTW")
                : "English"
          };
        });
      });
    },
    // 修改多语言
    updateOrgLang() {
      let data_update = [];
      for (const key in this.items) {
        data_update.push({
          id: this.items[key].id,
          langCode: this.items[key].langCode,
          orgId: this.orgId,
          orgName: this.items[key].orgName,
          version: this.items[key].version
        });
      }
      updateOrgNameLangList(data_update)
        .then(response => {
          this.$message.success({
            content: this.$t("module.orgManage.modifyOrgNameSuccess"),
            duration: 3
          });
        })
        .catch(error => {
          this.$message.error({
            content: error.message,
            duration: 3
          });
        });
      this.$emit("Close", data_update || []);
    }
  }
};
</script>
